# 用户行为监控系统修复验证脚本
# 测试前后端行为类型匹配和数据记录功能

Write-Host "=== 用户行为监控系统修复验证 ===" -ForegroundColor Green

# 1. 检查前端行为类型
Write-Host "`n1. 检查前端userBehaviorService.js中的行为类型..." -ForegroundColor Yellow
$frontendFile = "RefundAuditWeb/src/services/userBehaviorService.js"
if (Test-Path $frontendFile) {
    $frontendContent = Get-Content $frontendFile -Raw
    
    Write-Host "前端使用的行为类型:" -ForegroundColor Cyan
    $frontendActions = @()
    if ($frontendContent -match "trackAction\('([^']+)'") {
        $frontendActions += $matches[1]
    }
    
    # 提取所有trackAction调用
    $actionMatches = [regex]::Matches($frontendContent, "trackAction\('([^']+)'")
    foreach ($match in $actionMatches) {
        $action = $match.Groups[1].Value
        if ($frontendActions -notcontains $action) {
            $frontendActions += $action
        }
    }
    
    $frontendActions | ForEach-Object { Write-Host "  - $_" -ForegroundColor White }
} else {
    Write-Host "前端文件不存在: $frontendFile" -ForegroundColor Red
}

# 2. 检查后端行为类型常量
Write-Host "`n2. 检查后端ActionTypes常量定义..." -ForegroundColor Yellow
$backendFile = "Models/UserBehaviorModels.cs"
if (Test-Path $backendFile) {
    $backendContent = Get-Content $backendFile -Raw
    
    Write-Host "后端定义的行为类型:" -ForegroundColor Cyan
    if ($backendContent -match "public static class ActionTypes\s*\{([^}]+)\}") {
        $actionTypesBlock = $matches[1]
        $constantMatches = [regex]::Matches($actionTypesBlock, 'public const string (\w+) = "([^"]+)"')
        foreach ($match in $constantMatches) {
            $constantName = $match.Groups[1].Value
            $constantValue = $match.Groups[2].Value
            Write-Host "  - $constantName = $constantValue" -ForegroundColor White
        }
    }
} else {
    Write-Host "后端文件不存在: $backendFile" -ForegroundColor Red
}

# 3. 检查后端处理逻辑
Write-Host "`n3. 检查后端UpdateOperationSessionAsync方法..." -ForegroundColor Yellow
$serviceFile = "Services/UserBehaviorService.cs"
if (Test-Path $serviceFile) {
    $serviceContent = Get-Content $serviceFile -Raw
    
    Write-Host "后端处理的行为类型:" -ForegroundColor Cyan
    if ($serviceContent -match "switch \(actionType\)\s*\{([^}]+case[^}]+)\}") {
        $switchBlock = $matches[1]
        $caseMatches = [regex]::Matches($switchBlock, 'case ActionTypes\.(\w+):')
        foreach ($match in $caseMatches) {
            $actionType = $match.Groups[1].Value
            Write-Host "  - $actionType" -ForegroundColor White
        }
    }
} else {
    Write-Host "服务文件不存在: $serviceFile" -ForegroundColor Red
}

# 4. 检查IsKeyAction方法
Write-Host "`n4. 检查IsKeyAction方法中的关键行为..." -ForegroundColor Yellow
if (Test-Path $serviceFile) {
    $serviceContent = Get-Content $serviceFile -Raw
    
    Write-Host "关键行为类型:" -ForegroundColor Cyan
    if ($serviceContent -match "private static bool IsKeyAction\(string actionType\)\s*\{[^}]*return actionType switch\s*\{([^}]+)\}") {
        $switchBlock = $matches[1]
        $caseMatches = [regex]::Matches($switchBlock, 'ActionTypes\.(\w+) => true')
        foreach ($match in $caseMatches) {
            $actionType = $match.Groups[1].Value
            Write-Host "  - $actionType" -ForegroundColor White
        }
    }
}

# 5. 测试API端点
Write-Host "`n5. 测试用户行为监控API端点..." -ForegroundColor Yellow

# 检查后端服务是否运行
try {
    $response = Invoke-RestMethod -Uri "http://localhost:5000/api/health" -Method GET -TimeoutSec 5
    Write-Host "后端服务运行正常" -ForegroundColor Green
    
    # 测试用户行为跟踪API
    Write-Host "测试行为跟踪API..." -ForegroundColor Cyan
    $testData = @{
        actionType = "VIEW_PHONE"
        targetElement = "contact_button"
        timestamp = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
        sessionId = "test_session_$(Get-Date -Format 'yyyyMMddHHmmss')"
        additionalData = @{
            patientId = "test_patient"
            phoneNumber = "138****1234"
        }
    } | ConvertTo-Json -Depth 3
    
    try {
        $trackResponse = Invoke-RestMethod -Uri "http://localhost:5000/api/UserBehavior/track" -Method POST -Body $testData -ContentType "application/json" -TimeoutSec 10
        Write-Host "行为跟踪API测试成功" -ForegroundColor Green
    } catch {
        Write-Host "行为跟踪API测试失败: $($_.Exception.Message)" -ForegroundColor Red
    }
    
} catch {
    Write-Host "后端服务未运行或无法访问: $($_.Exception.Message)" -ForegroundColor Red
}

# 6. 检查前端Vue组件中的调用
Write-Host "`n6. 检查前端Vue组件中的userBehaviorService调用..." -ForegroundColor Yellow
$vueFile = "RefundAuditWeb/src/views/RefundTaskManagement.vue"
if (Test-Path $vueFile) {
    $vueContent = Get-Content $vueFile -Raw
    
    Write-Host "Vue组件中的行为监控调用:" -ForegroundColor Cyan
    $callMatches = [regex]::Matches($vueContent, 'userBehaviorService\.(\w+)\(')
    $uniqueCalls = @()
    foreach ($match in $callMatches) {
        $methodName = $match.Groups[1].Value
        if ($uniqueCalls -notcontains $methodName) {
            $uniqueCalls += $methodName
        }
    }
    
    $uniqueCalls | ForEach-Object { Write-Host "  - userBehaviorService.$_" -ForegroundColor White }
} else {
    Write-Host "Vue文件不存在: $vueFile" -ForegroundColor Red
}

Write-Host "`n=== 修复验证完成 ===" -ForegroundColor Green
Write-Host "请检查以上输出，确认前后端行为类型匹配，API正常工作。" -ForegroundColor Yellow
Write-Host "如果发现问题，请查看具体的错误信息进行进一步调试。" -ForegroundColor Yellow